package chan.project.smartlawcommon.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis")
@Data
@RefreshScope//动态刷新
public class RedissonConfig {

    private String host;

    private Integer database;

    private Integer port;
    //创建RedissonClient实例并交由spring管理
    @Bean
    public RedissonClient getRedissonClient(){
        Config config = new Config();
        //使用单机服务，不使用分布式
        config.useSingleServer()
                .setDatabase(database)
                .setAddress("redis://" + host + ":" + port);
        log.info("RedissonConfig: " + host + ":" + port + ":" + database);
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}